package com.example.mqtt.common;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class JSONUtil {
    private static final Logger log = LoggerFactory.getLogger(JSONUtil.class);

    public JSONUtil() {
    }

    public static String objectToJsonString(Object object) {
        String result = null;
        ObjectMapper objectMapper = new ObjectMapper();

        try {
            result = objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException var4) {
            log.error(object.getClass().getName() + "转String失败", var4);
        }

        return result;
    }

    public static <T> T stringToObject(String json, Class<T> classz) {
        T object = null;
        ObjectMapper objectMapper = new ObjectMapper();

        try {
            object = objectMapper.readValue(json, classz);
        } catch (IOException var5) {
            log.error(json + "转" + classz.getName() + "失败", var5);
        }

        return object;
    }
}
